import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class OverrideThreadPool {

    //重写线程池
    public static void main(String[] args) {
        MyThreadPool myThreadPool=new MyThreadPool(10);
        for (int i = 0; i < 10000; i++) {
            int n=i;
            myThreadPool.submit(new Runnable(){
                public void run(){
                    System.out.println("创建线程"+n);
                }
            });
        }
    }
}
//创建n个线程
class MyThreadPool{
    BlockingQueue<Runnable>queue=new LinkedBlockingQueue<Runnable>();
    public  MyThreadPool(int n){
        for(int i=0;i<n;i++){
            Thread thread=new Thread(()->{
                while(true){
                    try {
                        Runnable runnable= queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
        }
    }
public void submit(Runnable runnable)  {
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}



